# -*- codeing = utf-8 -*-
# @Time : 2022/8/17 19:02
# @Author : ZN
# @Email : 747723508@qq.com
# @File : tasks.py
# @Software: PyCharm
import logging

from django.core.mail import send_mail

from celery_tasks.celery_main import app
from de import settings
Logger = logging.getLogger(__name__)


@app.task(name='send_email_task')
def send_email_task(email,username,password,classes_name):
    try:
        email_param = {
            'subject': '测试发送',
            'message': '',
            'html_message':
                f"""
                    <h2>欢迎加入[{classes_name}]</h2> 
                    <p>您的登录用户名为<span style = "color:#116bb7">{username}</span></p>
                    <p>您的登录密码为<span style = "color:#116bb7">{password}</span></p>
                """,
            'from_email': settings.EMAIL_HOST_USER,  # 发件人
            'recipient_list': [email]  # 发送列表 可多发
        }
        res_send = send_mail(**email_param)
    except Exception as e :
        Logger.error('邮箱发送[异常][ email:%s,message:%s]'%(email,e))
    else:
        if res_send:
            Logger.info('邮箱发送[成功][ email:%s,username:%s]'%(email,username))
        else:
            Logger.warning('邮箱发送[失败][ email:%s,username:%s]'%(email,username))

